<!DOCTYPE html>
<html lang="en">
<head>
    <title>register</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript" src="../static/jquery.min.js"></script>
    <script>
    </script>
    <style>
        div{
            padding:10px 10px 0 10px;
        }
    </style>
</head>
<body>
<div>
    <form method="post" >
        <table>
            <tr>
                <td>邮件</td>
                <td><input type="text" id="email"/></td>
                <td></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="text" id="password"/></td>
                <td></td>
            </tr>
            <tr>
                <td>确认密码</td>
                <td><input type="text" id="confirm-password"/></td>
                <td></td>
            </tr>
            <tr>
                <td>验证码</td>
                <td><input type="text" id="code"/></td>
                <td><input type="button" value="获取验证码" id="getCode"/></td>
            </tr>
        </table>
        <input type="button" value="注册" id="submit"/>
    </form>
</div>
</body>
<script>
    $("#getCode").click(function () {
        let email = $("#email").val();
        $.ajax({
            url: '/api/user/isRegistered/'+email,
            type: 'GET',
            contentType: 'application/x-www-form-urlencoded',
            success: function (data) {
                if(data){
                    alert("this email registered");
                }else{
                    $.ajax({
                        url: '/api/code/create/'+email,
                        type: 'GET',
                        contentType: 'application/x-www-form-urlencoded',
                        success: function (data) {
                            alert("send success")
                        },
                        error:function () {
                            alert(textStatus);
                        }
                    })
                }
            },
            error:function () {
                alert("request error");
            }
        })
    });

    $("#submit").click(function () {
        let email = $("#email").val();
        let password = $("#password").val();
        let password1 = $("#confirm-password").val();
        if(password != password1){
            alert("password not eq confirm-password");
            return false;
        }
        $.ajax({
            url: '/api/user/isRegistered/'+email,
            type: 'GET',
            contentType: 'application/x-www-form-urlencoded',
            success: function (data) {
                if(data){
                    alert("this email registered");
                }else{
                    let code = $("#code").val();
                    $.ajax({
                        url: '/api/user/register/'+email+'/'+password+'/'+code,
                        type: 'GET',
                        contentType: 'application/x-www-form-urlencoded',
                        success: function (data) {
                            if(data){
                                window.open("/static/welcome.html?email="+email)
                            }else{
                                alert("validate code error");
                            }
                        },
                        error:function (XMLHttpRequest, textStatus, errorThrown) {
                            if(XMLHttpRequest.status == 303){
                                alert(XMLHttpRequest.responseText);
                            }else{
                                alert("request error");
                            }
                        }
                    })
                }
            },
            error:function () {
                alert("request error");
            }
        })
    })
</script>
</html>